#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author:Charles.chen
# createDate:2017/9/4
# Title:
# Tip:
from flask import Flask, current_app
from flask_login import LoginManager
from flask_bootstrap import Bootstrap
from app.toolkit.configSingle import ConfigSingle
import os

# 初始化中间件(init Middleware)
configObj = ConfigSingle()
bootstrap = Bootstrap()
login_manager = LoginManager()

# 加载顺序问题 先加载loginManager 再加载Quartz
from app.crontime.schedulerJob import Quartz
quartz = Quartz()

# 登录管理信息
login_manager.session_protection = 'hehenopwd'
login_manager.login_view = 'auth.login'
login_manager.login_message = "请登录！"

def create_app():

    app = Flask(__name__)
    app.secret_key = 'unable to guessing pwd!@#$%'
    # add default configObject path
    configObj.configuration.set("project_conf", "base_dir", os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)))
    configObj.flushConfig()

    bootstrap.init_app(app)
    login_manager.init_app(app)
    # 定时任务处理
    quartz.addJobDynamic()
    quartz.startJob()

    from app.model import initDb
    db = initDb()

    # register routes
    from .api import api as api_1_0_blueprint
    app.register_blueprint(api_1_0_blueprint, url_prefix='/api')
    # others
    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    from .guide import guideBp as guideBp_blueprint
    app.register_blueprint(guideBp_blueprint, url_prefix='/guide')

    from .system import sys as system_blueprint
    app.register_blueprint(system_blueprint, url_prefix='/sys')

    from .internet import netBp as netBp_blueprint
    app.register_blueprint(netBp_blueprint, url_prefix='/network')

    from .weichen import weichenBp as weichenBp_blueprint
    app.register_blueprint(weichenBp_blueprint, url_prefix='/weichen')

    return app

# 初始化方法信息
def initFunc():
    pass

if __name__ == '__main__':
    print "app is running"
    pass

